﻿using SharpShell.Attributes;
using SharpShell.SharpThumbnailHandler;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using Spire.Pdf;
using System.Windows.Forms;
using SharpShell.ServerRegistration;

namespace Tisn
{
    [ComVisible(true)]
    [COMServerAssociation(AssociationType.ClassOfExtension, ".pdf")]
    [Guid("350A359A-4A3D-4135-8204-673C51151E78")]
    public class PdfFileThumbnail : SharpThumbnailHandler
    {
        protected override Bitmap GetThumbnailImage(uint width)
        {
            GC.Collect();
            try
            {
                using (PdfDocument doc = new PdfDocument())
                {
                    doc.LoadFromStream(SelectedItemStream);
                    if (doc.Pages.Count > 0)
                    {
                        using (Bitmap tmp = new Bitmap(doc.SaveAsImage(0)))
                        {
                            Bitmap result = Utility.ResizeBitmap(tmp, (int)width);
                            return result;
                        }
                    }
                }
            }
            catch
            {
            }
            return null;
        }
        [PrevCustomRegisterFunction]
        internal static void P(Type type, RegistrationType registrationType)
        {
            Utility.PrevReg(type, registrationType, Utility.PDF_Names);
        }
    }
}
